package mk.jug.academy.controller;

import java.util.logging.Logger;

import javax.servlet.http.HttpServletRequest;

import mk.jug.academy.model.Course;
import mk.jug.academy.model.Lecturer;
import mk.jug.academy.model.Page;
import mk.jug.academy.model.Student;
import mk.jug.academy.model.Supporter;
import mk.jug.academy.service.CourseService;
import mk.jug.academy.service.LecturerService;
import mk.jug.academy.service.StudentService;
import mk.jug.academy.service.SupporterService;
import mk.jug.academy.util.Utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

/**
 * View layer coordinator for the navigation on the public facing frontend.
 *
 * @author Ice.Penov
 */
@Controller
public class NavigationController extends BaseController {
	
	public static final String WELCOME = "welcome";
	public static final String LOGIN = "login";
	public static final String ADMIN_HOME = "admin.home";
	public static final String LIST_AVAILABLE_COURSES = "public.list.available.courses";
	public static final String LIST_INPROGRESS_COURSES = "public.list.inprogress.courses";
	public static final String LIST_LECTURERS = "public.list.lecturers";
	public static final String LIST_WAITING_STUDENTS = "public.list.waiting.students";
	public static final String LIST_ENROLLED_STUDENTS = "public.list.enrolled.students";
	public static final String LIST_SUPPORTERS = "public.list.supporters";
	public static final String ABOUT = "public.about";
	public static final String SPONSOR_SECTION = "public.sponsor.section";
	public static final String EVENTS_SECTION = "public.events.section";
	
	private static final int itemsPerPage = 3;
	
	private static final Logger LOG = Logger.getLogger(NavigationController.class.getName());
		
	@Autowired private CourseService courseService;
	@Autowired private LecturerService lecturerService;
	@Autowired private StudentService studentService;
	@Autowired private SupporterService supporterService;
	
	@RequestMapping("/welcome.do")
	public String welcome(ModelMap map, HttpServletRequest request) {
		return WELCOME;
	}
	
	@RequestMapping("/about.do")
	public String about(ModelMap map, HttpServletRequest request) {
		return ABOUT;
	}
			
	@RequestMapping("/lecturers.do")
	public String lecturers(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Lecturer> pagedLecturers = lecturerService.listActive(page, itemsPerPage);
				map.addAttribute("lecturers", pagedLecturers.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedLecturers.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_LECTURERS;
			}
		}.result();
	}
	
	@RequestMapping("/students/waiting.do")
	public String waitingStudents(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Student> pagedStudents = studentService.listWaitingForEnrollment(page, itemsPerPage);
				map.addAttribute("students", pagedStudents.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedStudents.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_WAITING_STUDENTS;
			}
		}.result();
	}	
	
	@RequestMapping("/students/enrolled.do")
	public String enrolledStudents(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Student> pagedStudents = studentService.listEnrolled(page, itemsPerPage);
				map.addAttribute("students", pagedStudents.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedStudents.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_ENROLLED_STUDENTS;
			}
		}.result();
	}
	
	@RequestMapping("/supporters.do")
	public String supporters(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null){
					page = 1;
				}
				Page<Supporter> pagedSupporters = supporterService.listActive(page, itemsPerPage);
				map.addAttribute("supporters", pagedSupporters.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedSupporters.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_SUPPORTERS;
			}
		}.result();
	}
	
	@RequestMapping("/courses/available.do")
	public String available(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Course> pagedCourses = courseService.listAvailable(page, itemsPerPage);
				map.addAttribute("courses", pagedCourses.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedCourses.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_AVAILABLE_COURSES;
			}
		}.result();
	}
	
	@RequestMapping("/courses/inprogress.do")
	public String inProgress(final ModelMap map, final @RequestParam(value = "page", required = false) Integer p) {
		return new ControllerContext(map) {
			public String execute() {
				Integer page = p;
				if (page == null) {
					page = 1;
				}
				Page<Course> pagedCourses = courseService.listInProgress(page, itemsPerPage);
				map.addAttribute("courses", pagedCourses.getItems());
				map.addAttribute("pages", Utils.getPageNumber(itemsPerPage, pagedCourses.getTotalCount()));
				map.addAttribute("page", page);
				return LIST_INPROGRESS_COURSES;
			}
		}.result();
	}			
	
	@RequestMapping("/login.do")
	public String login() {
		return LOGIN;
	}
	
	@RequestMapping("/login/error.do")
	public String errorLogin(ModelMap map) {
		map.addAttribute("error", "true");
		return LOGIN;
	}
	
	@RequestMapping(value = "/admin/home.do")
	public String admin(ModelMap map) {
		return ADMIN_HOME;
	}		
	
	@RequestMapping(value = "/createNPE.do")
	public String createNPE(final ModelMap map) {
		return new ControllerContext(map) {
			@SuppressWarnings("null")
			public String execute() {
				String test = null;
				//create NPE to test your error page
				test.toString();
				return "willNotReachHere";
			}
		}.result();
	}
				
}